/**
 * 
 */
package com.impetus.client.crud.generatedId;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.impetus.client.crud.RDBMSCli;

/**
 * @author impadmin
 * 
 */
public class GeneratedIdTest
{
    private EntityManagerFactory emf;

    private EntityManager em;

    private RDBMSCli cli;

    /**
     * 
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
        createTables();
        emf = Persistence.createEntityManagerFactory("testHibernate");
        em = emf.createEntityManager();
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
        cli.update("DELETE FROM TESTDB.UserGeneratedId");
        cli.update("DROP TABLE TESTDB.UserGeneratedId");
        cli.update("DELETE FROM TESTDB.AddressGeneratedId");
        cli.update("DROP TABLE TESTDB.AddressGeneratedId");
        cli.dropSchema("TESTDB");
        em.close();
        emf.close();
        
        try
        {
            cli.update("DELETE FROM TESTDB.USERGENERATEDID");
            cli.update("DROP TABLE TESTDB.USERGENERATEDID");
            cli.update("DELETE FROM TESTDB.ADDRESSGENERATEDID");
            cli.update("DROP TABLE TESTDB.ADDRESSGENERATEDID");
            cli.update("DROP SCHEMA TESTDB");
            cli.closeConnection();
        }
        catch (SQLException e)
        {
            // do nothing..weird!!
        }
    }

    @Test
    public void test()
    {
        AddressGeneratedId address = new AddressGeneratedId();
        address.setStreet("sector 20, G Block");
        // address.setAddressId(99);

        UserGeneratedId user = new UserGeneratedId();
        // user.setPersonId(17);
        user.setAddress(address);
        user.setPersonName("Kuldeep");

        em.persist(user);

        em.clear();

        List<UserGeneratedId> result = em.createQuery("Select u from UserGeneratedId u").getResultList();
        Assert.assertNotNull(result);
        Assert.assertEquals(1, result.size());
        Assert.assertNotNull(result.get(0).getAddress());
        Assert.assertEquals("sector 20, G Block", result.get(0).getAddress().getStreet());
        Assert.assertEquals("Kuldeep", result.get(0).getPersonName());
    }

    private void createTables() throws SQLException
    {

        try
        {
            cli = new RDBMSCli("testdb");
            cli.createSchema("testdb");
        }
        catch (Exception e)
        {

        }
        try
        {
            cli.update("CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1");
            cli.update("CREATE TABLE TESTDB.USERGENERATEDID (PERSON_ID INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1), PERSON_NAME VARCHAR(256), ADDRESS_ID INTEGER)");
        }
        catch (Exception e)
        {
            cli.update("DELETE FROM TESTDB.USERGENERATEDID");
            cli.update("DROP TABLE TESTDB.USERGENERATEDID");
            cli.update("CREATE TABLE TESTDB.USERGENERATEDID (PERSON_ID INT  GENERATED BY DEFAULT AS IDENTITY (START WITH 1), PERSON_NAME VARCHAR(256), ADDRESS_ID INTEGER)");
        }
        try
        {
            cli.update("CREATE TABLE TESTDB.ADDRESSGENERATEDID (ADDRESS_ID INTEGER  GENERATED BY DEFAULT AS IDENTITY (START WITH 1), STREET VARCHAR(256))");

        }
        catch (Exception e)
        {
            cli.update("DELETE FROM TESTDB.ADDRESSGENERATEDID");
            cli.update("DROP TABLE TESTDB.ADDRESSGENERATEDID");
            cli.update("CREATE TABLE TESTDB.ADDRESSGENERATEDID (ADDRESS_ID INTEGER  GENERATED BY DEFAULT AS IDENTITY (START WITH 1), STREET VARCHAR(256))");
        }
    }

}
